<?php

session_start();

require_once('LoginForm.php');

echo '<!doctype html>
	<html lang="sv">
		<head>
			<meta charset="utf-8" />
			<title> Lab1 </title>
		</head>
		<body>
			<h1> Laboration 1 - no222bd </h1>';

if(isset($_POST['logout'])) {
	unset($_SESSION['inloggad'], $_SESSION['user']);
	$_SESSION['status']="Du har nu loggat ut";
}

if(isset($_POST['submitted'])){

	$uname = $_POST['uname'];
	$pwd = $_POST['pwd'];
	$users = array('nisse' => '1234', 'sara' => '5678', 'admin' => 'password');

	if($uname != "" && $pwd != "") {
		if(array_key_exists($uname, $users) && $users[$uname] == $pwd) {
				$_SESSION['inloggad']=1;
				$_SESSION['status'] = "Inloggning lyckades";
			} else {
				$_SESSION['status'] = "Felaktigt användarnamn och/eller lösenord";
			}
	} else if($uname == "") {
		$_SESSION['status'] = "Användarnamn saknas";
	} else {
		$_SESSION['status'] = "Lösenord saknas";
	}
	$_SESSION['user'] = $uname;
}

if(isset($_SESSION['inloggad'])){
	echo "<h2> " . $_SESSION['user'] . " är inloggad </h2>";
	if(isset($_SESSION['status'])) {
		echo "<h3>" . $_SESSION['status'] . "</h3>";
	}
	echo '<form method="post">
			<input type="submit" name="logout" value="Logout" />
			</form>';
	unset($_SESSION['status']);
} else {	
	echo "<h2> Ej inloggad </h2>";
	$FormView = new LoginForm();
	echo $FormView->getForm();
	if(isset($_SESSION['status'])) {
		echo "<h3>" . $_SESSION['status'] . "</h3>";
	}
	unset($_SESSION['status']);
}

switch(date('w')){
	case 0:
		$weekday = "Söndag";
		break;
	case 1:
		$weekday = "Måndag";
		break;
	case 2:
		$weekday = "Tisdag";
		break;
	case 3:
		$weekday = "Onsdag";
		break;
	case 4:
		$weekday = "Torsdag";
		break;
	case 5:
		$weekday = "Fredag";
		break;
	case 6:
		$weekday = "Lördag";
		break;
}

switch(date('n')){
	case 1:
		$month = "Januari";
		break;
	case 2:
		$month = "Februari";
		break;
	case 3:
		$month = "Mars";
		break;
	case 4:
		$month = "April";
		break;
	case 5:
		$month = "Maj";
		break;
	case 6:
		$month = "Juni";
		break;
	case 7:
		$month = "Juli";
		break;
	case 8:
		$month = "Augusti";
		break;
	case 9:
		$month = "September";
		break;
	case 10:
		$month = "Oktober";
		break;
	case 11:
		$month = "November";
		break;
	case 12:
		$month = "December";
		break;
}

echo "<p> $weekday, den " . date('j') . " $month år " . date('Y') . ". Klockan är [" . date('G:i:s') . "]. </p>";

echo '</body>
	</html>';